package com.dl.dw.utils;

import static android.content.Context.WINDOW_SERVICE;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ConvertUtils;

public class OverlayUtil {
    public static boolean checkPermission(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                && !Settings.canDrawOverlays(activity)) {
            Toast.makeText(activity, "当前无权限，请授权", Toast.LENGTH_SHORT).show();
            activity.startActivityForResult(
                    new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                            Uri.parse("package:" + activity.getPackageName())), 0);
            return false;
        }
        return true;
    }

    public static void showOverlay(Activity activity, View view,WindowManager.LayoutParams params){
//        TextView showTextView = new TextView(this);
//        showTextView.setText("Hello World");
//        // WindowManager.LayoutParams.WRAP_CONTENT,
//        //                WindowManager.LayoutParams.WRAP_CONTENT,
//        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
//                ConvertUtils.dp2px(300),
//                ConvertUtils.dp2px(100),
//                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
//                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
//                PixelFormat.TRANSLUCENT);
//        params.gravity = Gravity.TOP | Gravity.RIGHT;

        checkPermission(activity);
        WindowManager wm = (WindowManager) activity.getSystemService(WINDOW_SERVICE);
        wm.addView(view, params);
    }
}
